/* Actions-Catch
 * ****************************************************************************/
$('#mapaInicio').click(function(){
    mapaInicio();
});
$('#novedades_btn').click(novedadesMapasMenu);
$('#riesgos_btn').click(riesgosMapasMenu);
$('#extintores_btn').click(extintoresMapasMenu);
$('#rutaE_btn').click(rutaEscapeMapasMenu);

/* Interface-Loaders
 * ****************************************************************************/
function novedadesMapasMenu(){
    $('.current_menu_item2').removeClass('current_menu_item2');
    $('#novedades_btn').addClass('current_menu_item2');

    $('#center').load('Modulo2/mapNovedad.jsp');
    $('#sidebar').load('Modulo2/controls/controlNovedades.jsp');
    return false;
}
function riesgosMapasMenu(){
    $('.current_menu_item2').removeClass('current_menu_item2');
    $('#riesgos_btn').addClass('current_menu_item2');

    $('#center').load('Modulo2/mapRiesgo.jsp');
    $('#sidebar').load('Modulo2/controls/controlRiesgos.jsp');
    return false;
}
function extintoresMapasMenu(){
    $('.current_menu_item2').removeClass('current_menu_item2');
    $('#riesgos_btn').addClass('current_menu_item2');

    $('#sidebar').load('Modulo2/controls/controlPiso.jsp');
    $('#center').load('Modulo2/gestionExtintores.jsp');
    return false;
}
function rutaEscapeMapasMenu(){
    $('.current_menu_item2').removeClass('current_menu_item2');
    $('#rutaE_btn').addClass('current_menu_item2');

    $('#sidebar').load('Modulo2/Templates/menuRutaEscape.jsp');
    $('#center').load('Modulo2/Templates/inicioMapas.jsp');
    return false;
}
/* Actions
 * ****************************************************************************/
function listEdificios(){
    $('#edificios').html("");
    $.ajax({
        url: "listaEdificioAction.do",
        type: "GET",
        dataType: 'json',
        success: function(data) {
            $.each(data.Edificio, function(i,item){
                string = "<option value='"+item.edificio+"' >"+item.edificio+"</option>";
                $('#edificios').prepend(string);
            });
            $('#edificios').prepend("<option value='' selected></option>")
        }
    });
}
function getPisos(edificio){
    $.ajax({
        type: "GET",
        url: "getPisos.do?edificio="+edificio,
        dataType: 'json',
        success: processGetPisos
    });
    return false;
}
function getPisosSelect(edificio){
    $.ajax({
        type: "GET",
        url: "getPisos.do?edificio="+edificio,
        dataType: 'json',
        success: processGetPisosSelect
    });
    return false;
}
function fileUpload(idForm){
    $('#'+idForm).submit();
    $('#returnMsg').css('display','block');
    return false;
}
/* Process-Actions
 * ****************************************************************************/
function processGetPisosSelect(answer){
    if(answer.pisos){
        $('#pisos').html("<option value='' selected></option>");
        $.each(answer.pisos, function(index,value){
            if(value.id_piso == "0") piso = "PB";
            else piso = value.id_piso;
            string = "<option value='"+value.id_piso+"' >"+piso+"</option>";
            $('#pisos').append(string);
        });
    }
}
function processGetPisos(answer){
    if(answer.pisos){
        ps = "";
        $.each(answer.pisos, function(index,value){
            params = "'"+answer.edificio+"','"+value.id_piso;
            action = "\"javascript:loadPlano("+params+"');\""
            if(value.id_piso > 0)
                ps += "   |   <a onclick="+action+">"+value.id_piso+"</a>";
            else
                ps = "<a onclick="+action+">PB</a>" + ps;
        });
        $('#pisos').html(ps);
        $('#backUSB').toggleClass('muestra');
    }
}
function controlsTree(){
    div = $("<div style='float:left; clear:both; margin-top: 20px; width: 100%'>");
    table = $("<table style='float:left; clear:both; margin-top: 20px; width: 100%'>");
    tr = $("<tr>");
    tr.append($("<td>").append($("<a href='javascript:void(0);' id='allChecks'>").append("<img alt='' src='style/imagesTree/check-all.gif'/> Todos")));
    tr.append($("<td>").append($("<a href='javascript:void(0);' id='resetChecks'>").append("<img alt='' src='style/imagesTree/uncheck-all.gif'/> Ninguno")));
    table.append(tr);
    tr = $("<tr>");
    tr.append($("<td>").append("<input type='submit' value='Consultar' class='controlButton'/>"));
    table.append(tr);
    div.append(table);
    return div.html();
}